local RoomBase = require 'packages.chess-games.xiangqi.core.roombase'

---@class Xiangqi.Client: Xiangqi.RoomBase, ClientBase
local Client = RoomBase:subclass("Xiangqi.Client")
local Xq = require "packages.chess-games.xiangqi"

Client:include(Fk.Base.ClientBase)

local ClientPlayer = require "packages.chess-games.xiangqi.client.clientplayer"

function Client:initialize(_client)
  RoomBase.initialize(self)
  Fk.Base.ClientBase.initialize(self, _client)

  self.clientplayer_klass = ClientPlayer

  self:addCallback("Xiangqi.SyncBoard", self.syncBoard, true)
  self:addCallback("Xiangqi.MovePieceTo", self.handleMovePieceTo, true)
  self:addCallback("Xiangqi.PlayChess", self.playChess, true)
end

function Client:syncBoard(data)
  self.board = data
end

function Client:handleMovePieceTo(data)
  local pieceId, fromX, fromY, x, y = data[1], data[2], data[3], data[4], data[5]
  self:movePieceTo(pieceId, fromX, fromY, x, y)
  if Xq:isChecking(self.board, Self.role) then
    self:notifyUI("Xiangqi.PlaySound", "check")
  else
    self:notifyUI("Xiangqi.PlaySound", "play")
  end
end

function Client:playChess(data)
  self:setupRequestHandler(Self, "Xiangqi.PlayChess", data)
end

function Client:sendDataToUI(data)
  self:notifyUI("Xiangqi.SyncBoard", self.board)
end

return Client
